///
//  KKBLEAccept.m
//
//  Created by zorro on 2017/11/13.
//  Copyright © 2017年 zorro. All rights reserved.
//

#import "KKBLEAccept.h"
#import "KKBLEService.h"
#import "FileModelEntity.h"
@implementation KKBLEAccept
- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)updateData:(NSData *)data {
    UInt8 val[20] = {0};
    [data getBytes:&val length:data.length];
    NSString *content = [NSString stringWithFormat:@"%@", data];
    id       object   = data;
    // 时间
    if (val[1] == 0x02) {
        if (val[2] == 0x01) {
            _acceptType = KKBLEAcceptTypeTime;
            LogWrite(nil, content, @"设置时间成功");
            NSLog(@"设置时间成功>>>>");
        }
    }
    // MAC地址
    if (val[1] == 0x01) {
        if (val[2] == 0x01) {
            NSString *mac = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", val[3], val[4], val[5], val[6], val[7], val[8]];
            KK_BLEModel.bleMAC = mac;
            [KK_BLEModel updateToDB];
            LogWrite(nil, content, ([NSString stringWithFormat:@"获取设备mac地址成功:%@", mac]));
            NSLog(@"获取设备mac地址>>>>%@", mac);
            if (mac) {
                [KK_BLEModel.bleUUID setObjectValue:mac];
            }
            [SN_USER updateToDB];
            _acceptType = KKBLEAcceptTypeDevideMAC;
        }
    }
    // 设备信息
    if (val[1] == 0x01) {
        if (val[2] == 0x02) {
            LogWrite(nil, content, @"获取设备信息成功");
            NSLog(@"获取设备信息>>>>");
            KK_BLEModel.deviceID        = val[3] * 256 + val[4];   // 设备ID;
            KK_BLEModel.bleVersion      = val[5];                  // 固件版本
            KK_BLEModel.batteryQuantity = val[7];                  // 电池电量
            int customerID = val[8] * 256 + val[9];
            if (customerID >0) {
                KK_BLEModel.bleCustomerID = customerID;
            }
            SN_USER.bleBatteryQuantity = val[7];
            if (KK_BLEModel.bleCustomerID == 1) {
                SN_USER.heartFunction = intConvertBit(val[10], 0);
                SN_USER.bpFunction    = intConvertBit(val[10], 1);
                SN_USER.oxFunction    = intConvertBit(val[10], 2);
            } else {
                SN_USER.heartFunction = NO;
                SN_USER.bpFunction    = NO;
                SN_USER.oxFunction    = NO;
            }
            NSLog(@"SN_USER.heartFunction>>>>%d bleCustomerID %d", SN_USER.heartFunction, KK_BLEModel.bleCustomerID);
            SN_USER.updateBleCustomerID = KK_BLEModel.bleCustomerID;
            [KK_BLEModel updateToDB];
            [SN_USER updateToDB];
            _acceptType = KKBLEAcceptTypeDevideInfo;
            [[FileModelEntity sharedInstance] checkUpdateFirmwareInfo];
        }
    }
    
    if (val[1] == 0x02) {
        if (val[2] == 0x02) {
            _acceptType = KKBLEAcceptTypeUserInfo;
            LogWrite(nil, content, @"用户信息设置成功");
            NSLog(@"用户信息设置成功>>>");
        } else if (val[2] == 0x04) {
            _acceptType = KKBLEAcceptTypeAlarmClock;
            LogWrite(nil, content, @"闹钟设置成功");
        } else if (val[2] == 0x05) {
            _acceptType = KKBLEAcceptTypeCalendar;
            LogWrite(nil, content, @"日程设置成功");
        } else if (val[2] == 0x06) {
            _acceptType = KKBLEAcceptTypeDrink;
            LogWrite(nil, content, @"喝水提醒设置成功");
            NSLog(@"喝水提醒设置成功>>>");
        } else if (val[2] == 0x07) {
            _acceptType = KKBLEAcceptTypeSedentary;
            LogWrite(nil, content, @"久坐提醒设置成功");
            NSLog(@"久坐提醒设置成功>>>");
        } else if (val[2] == 0x09) {
            _acceptType = KKBLEAcceptTypeNotify;
            LogWrite(nil, content, @"消息推送设置成功");
            NSLog(@"消息推送设置成功>>>");
        } else if (val[2] == 0x0a) {
            if (val[3] == 0x01) {
                _acceptType = KKBLEAcceptTypeOther;
                LogWrite(nil, content, @"低电量以及其他设置");
                NSLog(@"低电量以及其他设置成功>>>");
            }
        } else if (val[2] == 0x0b) {
            if (val[3] == 0x01) {
                _acceptType = KKBLEAcceptTypeOther;
                LogWrite(nil, content, @"免打扰设置成功");
                NSLog(@"免打扰设置成功>>>");
            }
        }
    }
    
    if (val [1] == 0x05) {
        if (val[2] == 0x03 && val[3] == 0x01) {
            //            _acceptType = KKBLEAcceptTypeFindBle;
            LogWrite(nil, content, @"寻找手环成功");
        }
    }
    
    //拍照
    if (val[1] == 0x06) {
        if (val[2] == 0x01) {
            [SN_PublicClass.BLTControlTDelegate bltControlTakePhoto];
            LogWrite(nil, content, @"接收手环拍照指令");
        }
        if (val[2] == 0x02) {
            [SN_PublicClass.BLTControlTDelegate bleControlFindPhone];
            LogWrite(nil, content, @"接收手环找手机指令");
        }
    }
    
    // 实时步数
    if (val[1] == 0x07) {
        if (val[2] == 0x01) {
            if (data.length > 3) {
                _acceptType = KKBLEAcceptTypeRealtimeSport;
                [SN_PublicClass.todaySport updateRealStepData:data];
            }
        }
    }
    
    // 大数据 1 运动
    if (val[1] == 0x07) {
        if (val[2] == 0x03) {
            //记步大数据
            [SportModel updateSportData:data];
        } else if (val[2] == 0x05) {
            //距离大数据
            [SportModel updateSportData:data];
        } else if (val[2] == 0x06) {
            //卡路里大数据
            [SportModel updateSportData:data];
        } else if (val[2] == 0xFF) {
            [SportModel sportTransEnd];
            // 运动结束
            _acceptType = KKBLEAcceptTypeDataSport;
        }
    }
    
    if (val[1] == 0x07) {
        if (val[2] == 0x04) {
            [SleepModel updateSleepData:data];
        } else if (val[2] == 0xFE) {
            // 睡眠结束
            [SleepModel sleepTransEnd];
            _acceptType = KKBLEAcceptTypeDataSleep;
        }
    }
    
    if (val[1] == 0x07) {
        if (val[2] == 0x07) {
            [HeartModel updateHeartData:data];
        } else if (val[2] == 0xFD) {
            // 心率
            [HeartModel heartTransEnd];
            _acceptType = KKBLEAcceptTypeDataHeart;
        }
    }
    if (val[1] == 0x07) {
        if (val[2] == 0x02) {
            if (SN_PublicClass.checkType == 1 && val[3] > 0) {
                [SN_PublicClass.todayHeart updateRealHeartData:val[3]];
                _acceptType = KKBLEAcceptTypeRealtimeHeart;
            }
            if (SN_PublicClass.checkType == 2 && val[4] > 0) {
                [SN_PublicClass.todayOx updateRealOXData:val[4]];
                _acceptType = KKBLEAcceptTypeRealtimeOX;
            }
            if (SN_PublicClass.checkType == 3 && val[5] > 0 && val[6] > 0) {
                [SN_PublicClass.todayBp updateRealBPData:val[5] withBp:val[6]];
                _acceptType = KKBLEAcceptTypeRealtimeBP;
            }
        }
    }
    if (_acceptStatus) {
        _acceptStatus(object, _acceptType);
    }
    if (KK_BLEConnect.bleConnectAcceptBlock) {
        KK_BLEConnect.bleConnectAcceptBlock(object, _acceptType);
    }
}

@end

